package com.pyip.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pyip.reggie.Entity.Category;
import com.pyip.reggie.Entity.Dish;
import com.pyip.reggie.Entity.Setmeal;
import com.pyip.reggie.common.R;
import com.pyip.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> addCategory(@RequestBody Category category){
        return categoryService.save(category) ? R.success("添加分类成功") : R.error("添加分类失败");
    }
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    public R<Page<Category>> getPage(int page, int pageSize){
        return categoryService.getPage(page,pageSize);
    }
    @RequestMapping(method = RequestMethod.DELETE)
    public R<String> deleteById(Long id){
        return categoryService.deleteById(id);
    }
    @RequestMapping(method = RequestMethod.PUT)
    public R<String> updateById(@RequestBody Category category){
        return categoryService.updateById(category) ? R.success("修改成功") : R.error("修改失败");
    }

    /**
     * 根据菜品的种类=返回（也可以用String type接收参数）
     * @param category
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public R<List<Category>> getCategoryByType(Category category){
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        categoryLambdaQueryWrapper.orderByAsc(Category::getSort);
        return R.success(categoryService.list(categoryLambdaQueryWrapper));
    }


}
